﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Collections;

/// <summary>
/// Topic
/// </summary>
public class Topic
{
    DBControl dbcontrol = new DBControl();

    #region 查询主题，返回数据集
    /// <summary>
    /// 查询主题，返回数据集
    /// </summary>
    /// <returns>ds</returns>
    public DataSet GetTopicList()
    {
        string strCMD = "SELECT * FROM tb_TopicInfo ORDER BY CreateDate DESC";
        DataSet ds = dbcontrol.GetDataSet(strCMD,"TopicList");
        return ds;
    }
    #endregion

    #region 发表新主题
    /// <summary>
    /// 发表新主题
    /// </summary>
    /// <param name="strUserName"></param>
    /// <param name="strTopicTitle"></param>
    /// <param name="strTopicContent"></param>
    /// <param name="dtCreateDate"></param>
    /// <param name="strUserIP"></param>
    /// <returns>iResult</returns>
    public int AddTopic(string strUserName, string strTopicTitle,
        string strTopicContent, DateTime dtCreateDate, string strUserIP)
    {
        string strCMD = "INSERT INTO tb_TopicInfo(UserName,TopicTitle,TopicContent,CreateDate,UserIP)"
            +" VALUES('"+strUserName+"','"+strTopicTitle
            +"','"+strTopicContent+"','"+dbcontrol.DateTimeToString(dtCreateDate)
            +"','"+strUserIP+"')";
        int iResult = dbcontrol.SqlExecuteNonQuery(strCMD);
        return iResult;
    }
    #endregion

    #region 通过TopicID查询主题
    /// <summary>
    /// 通过TopicID查询主题
    /// </summary>
    /// <param name="iTopicID"></param>
    /// <returns>dr</returns>
    public SqlDataReader GetTopicDetail(int iTopicID)
    {
        string strCMD = "SELECT * FROM tb_TopicInfo WHERE TopicID='"+iTopicID+"'";
        SqlDataReader dr = dbcontrol.GetDataReader(strCMD);
        return dr;
    }
    #endregion

    #region 修改主题
    /// <summary>
    /// 修改主题
    /// </summary>
    /// <param name="iTopicID"></param>
    /// <param name="strTopicTitle"></param>
    /// <param name="strTopicContent"></param>
    /// <returns>iResult</returns>
    public int ModifyTopic(int iTopicID, string strTopicTitle, string strTopicContent)
    {
        string strCMD = "UPDATE tb_TopicInfo SET TopicTitle='" + strTopicTitle
            + "',TopicContent='" + strTopicContent + "' WHERE TopicID='" + iTopicID + "'";
        int iResult = dbcontrol.SqlExecuteNonQuery(strCMD);
        return iResult;
    }
    #endregion

    #region 删除主题
    /// <summary>
    /// 删除主题
    /// </summary>
    /// <param name="iTopicID"></param>
    /// <returns>iResult</returns>
    public bool DeleteTopic(int iTopicID)
    {
        ArrayList arrSQLs = new ArrayList();
        string strCMD1 = "DELETE FROM tb_TopicInfo WHERE TopicID='"+iTopicID+"'";
        arrSQLs.Add(strCMD1);
        string strCMD2 = "DELETE FROM tb_ReplyInfo WHERE TopicID='"+iTopicID+"'";
        arrSQLs.Add(strCMD2);
        bool bIsSuccess=dbcontrol.ExecuteSQLs(arrSQLs);
        return bIsSuccess;
    }
    #endregion
}
